<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {


	var $orgid=0;

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));

		$orgname = $this->uri->segment(3, 0);
		$query_string="select id from users where username='".$orgname."'";
		$orgidresult=$this->db->query($query_string);
		foreach($orgidresult->result() as $row){
			$this->orgid=$row->id;
		}
		if($this->orgid > 0){
			$this->session->set_userdata('orgid',$this->orgid);
		}

	}

	function view(){
		//Get the page id and pass it to the view.
		date_default_timezone_set('Asia/Calcutta');


		$orgname = $this->uri->segment(3, 0);

		$orgid=0;
		$query_string="select id from users where username='".$orgname."'";
		$orgidresult=$this->db->query($query_string);
		foreach($orgidresult->result() as $row){
			$orgid=$row->id;
		}


		$query=$this->db->select('*')->from('guiattributes')->where("orgid",$orgid)->get();
		$totalcharges=$this->input->post('total-hidden-charges',TRUE);

		$data=array();
		foreach($query->result() as $row){
			$data[$row->key]=$row->val;
		}

		$linksquery=$this->db->select('*')->from('categories')->where("orgid",$orgid)->where("order >",0)->order_by('order')->get();

		$links=array();
		foreach($linksquery->result() as $row){
			$links[$row->name]=$row->id;
		}

		$data['links']=$links;

		$pagelink=$this->uri->segment(4,0);
		if(is_null($pagelink)){
			$pagelink='cwelcome';
		}

		$data['pagelink']=$pagelink;
		$data['totalcharge']=$totalcharges;

		//		$this->load->view('header2');
		// 		$this->load->view('customerheader',$data);
		// 		$this->load->view('twocol',$data);
		// 		$this->load->view('customfooter');
		$this->load->view('cart/mainpage',$data);
	}


	public function loadpage(){
		$page=$this->input->post("page",TRUE);
		$data['totalcharge']=$this->input->post("totalcharge",TRUE);
		$data['catid']=$page;

		if(is_numeric($page)){
			$this->load->view('cart/itemsview',$data);
		}else{
			$this->load->view($page,$data);
		}


	}

	function fileupload()
	{
		$this->load->view('fileuploader', array('error' => ' ' ));
	}

	function do_upload()
	{
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png|JPEG|PNG|JPG|jpeg|GIF';
		$config['max_size']	= '1000';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		$this->load->library('upload', $config);
			
		if ( ! $this->upload->do_upload())
		{
			echo $this->upload->display_errors();
		}
		else
		{
			echo "Upload Successfull";
		}
	}

	public function additem(){

		$data=$this->input->post();
		$id=$this->input->post('id',TRUE);

		$cart_string=$this->session->userdata('cart');
		$cart=json_decode($cart_string);

		if(!is_null($cart)) {
			if(array_key_exists($id,$cart)){
				echo "Key exists";
				$datatmp=$cart->$id;
				$datatmp->count +=1;
				print_r($datatmp->count);
				// 				echo "Quantity for ".$id." is ".$datatmp['price'];
				// 				$cart->$id=$datatmp;
					
			}else{
				echo "Key doesnt exist";
				$cart->$id=$data;
					
			}
			//$cart[$id]=$data;
		}else{
			echo "Its null add it";
			$cart->$id=$data;
		}

		$cartjsonstr=json_encode($cart);
		$this->session->set_userdata('cart',$cartjsonstr);
	}

	public function getitems(){

		$cartstr=$this->session->userdata('cart');

		if($cartstr == ''){
			echo json_encode(array());
		}else{
			echo $cartstr;
		}

	}

	public function delitem(){
		$id=$this->input->post('id',TRUE);
		$cart_string=$this->session->userdata('cart');
		$cart=json_decode($cart_string);
		unset($cart->$id);
		$cartjsonstr=json_encode($cart);
		$this->session->set_userdata('cart',$cartjsonstr);
	}

	function gethtml(){
		//$wfid=$this->input->post("wfid",TRUE);

		$query=$this->db->query('select WorkflowID from workflow where ActiveInd=1 and OrgID=1');
		$wfid=3;

		if($query->num_rows()>0){
			$row = $query->row();
			$wfid=$row->WorkflowID;
		}
		
		$this->load->model('workflowmodel','workflow');
		$dataobj=$this->workflow->getworkflowarray($wfid);

		foreach($dataobj['service_array'] as $service){

			$this->workflow->fetchhtmlfromservice($service['descr']);
		}
	}

	public function validatecard(){
		$custid=$this->session->userdata('cust_id');
		$data=array(
					'Email'=>$this->input->post('email'),
					'Phone'=>$this->input->post("fname",TRUE).' '.$this->input->post("lname",TRUE),
					'CreditCardNo'=>$this->input->post('ccno'),
					'OrderTotal'=>$this->input->post('amount')
		);
		$this->db->where('CustomerID', $custid);
		$this->db->update('customer', $data);
		return true;
	}

	public function shipping(){
		$data=array(
			'OrgID'=>$this->orgid,
			'Name'=>$this->input->post("fname",TRUE).' '.$this->input->post("lname",TRUE),
			'address1'=>$this->input->post('add1'),
		    'address2'=>$this->input->post('add2'),
			'city'=>$this->input->post('city'),
			'state'=>$this->input->post('state'),
			'ShoppingCart'=>$this->session->userdata('cart')
		);
		$this->db->insert('customer',$data);
		$insertid=$this->db->insert_id();

		$this->session->set_userdata('cust_id',$insertid);

		$text = "Shipping address:<br>";
		$text = $text.$this->input->post("fname",TRUE).' '.$this->input->post("lname",TRUE)."</br>";
		$text = $text.$this->input->post('add1')."</br>";
		$text = $text.$this->input->post('add2')."</br>";
		$text = $text.$this->input->post('city')."</br>";
		$text = $text.$this->input->post('state')."</br>";

		echo json_encode(array('status'=>1,'content'=>$text));

	}

	public function confirmation(){
		$this->session->sess_destroy();
		return true;
	}

	public function get_all_items_by_cat(){

		$catid=$this->input->post("catid",TRUE);

		$request = xmlrpc_encode_request("get_items_by_cat", array($this->session->userdata('orgid'),$catid));
		$context = stream_context_create(array('http' => array(
				    'method' => "POST",
				    'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
				    'content' => $request
		)));

		$server = 'http://localhost/sunshopwebsvc/xmlrpc.php';

		$file = file_get_contents($server, false, $context);

		$response = xmlrpc_decode($file);

		echo json_encode($response);
	}

}